package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.entity.SystemMenu;
import com.woniuxy.factory.exception.MenuException;
import com.woniuxy.factory.service.SystemMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class SystemMenuController {

    @Autowired
    SystemMenuService systemMenuService;


    @RequestMapping("/menu/list.do")
    @ResponseBody
    public  Map<String,Object> list(){
        return systemMenuService.findMenus();
    }

    @RequestMapping("/menu/showAll.do")
    @ResponseBody
    public ResponseResult showAllMenu(){
        return systemMenuService.showAllMenu();
    }

    @RequestMapping("/menu/add.do")
    @ResponseBody
    public ResponseResult addNewMenu(SystemMenu menu,String menus){
        //创建容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(menu.getTitle()==null || menu.getTitle().equals("")){
            result.setCode(1);
            result.setMsg("菜单名称不可为空");
            return result;
        }
        if(menu.getIcon()==null || menu.getIcon().equals("")){
            result.setCode(1);
            result.setMsg("菜单图标不可为空");
            return result;
        }
        //业务逻辑处理
        try {
            result = systemMenuService.addMenu(menu,menus);
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        return result;
    }

    @RequestMapping("/menu/edit.do")
    @ResponseBody
    public ResponseResult editMenu(SystemMenu menu,String menus){
        System.out.println(menu);
        System.out.println(menus);
        //创建容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(menu.getTitle()==null || menu.getTitle().equals("")){
            result.setCode(1);
            result.setMsg("菜单名称不可为空");
            return result;
        }
        if(menu.getIcon()==null || menu.getIcon().equals("")){
            result.setCode(1);
            result.setMsg("菜单图标不可为空");
            return result;
        }
        //业务逻辑处理
        try {
            result = systemMenuService.editMenu(menu,menus);
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        return result;
    }

    @RequestMapping("/menu/selectParent.do")
    @ResponseBody
    public ResponseResult selectParent(Integer id){
        //初始化容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(id == null){
            result.setCode(1);
            result.setMsg("参数有误，请重试");
            return result;
        }
        try {
            //页面逻辑处理
            result.setData(systemMenuService.selectParentForAdd(id));
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setCode(0);
        return result;
    }
    @RequestMapping("/menu/getMenu.do")
    @ResponseBody
    public ResponseResult getMenu(Integer id){
        //初始化容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(id == null){
            result.setCode(1);
            result.setMsg("参数有误，请重试");
            return result;
        }
        try {
            //页面逻辑处理
            result.setData(systemMenuService.getMenu(id));
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setCode(0);
        return result;
    }

    @RequestMapping("/menu/up.do")
    @ResponseBody
    public ResponseResult upSort(Integer id){
        //初始化容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(id == null){
            result.setCode(1);
            result.setMsg("参数有误，请重试");
            return result;
        }
        //业务逻辑
        try {
            //修改列判断
            if (systemMenuService.upSort(id)==0){
                result.setCode(1);
                result.setMsg("修改失败，请重试");
                return result;
            }
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setCode(0);
        result.setMsg("修改成功");
        return result;
    }

    @RequestMapping("/menu/down.do")
    @ResponseBody
    public ResponseResult downSort(Integer id){
        //初始化容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(id == null){
            result.setCode(1);
            result.setMsg("参数有误，请重试");
            return result;
        }
        //业务逻辑
        try {
            //修改列判断
            if (systemMenuService.downSort(id)==0){
                result.setCode(1);
                result.setMsg("修改失败，请重试");
                return result;
            }
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setCode(0);
        result.setMsg("修改成功");
        return result;
    }

    @RequestMapping("/menu/sortEnd.do")
    @ResponseBody
    public ResponseResult endSort(Integer pid){
        //初始化容器
        ResponseResult result = new ResponseResult();
        //非空判定
        if(pid == null){
            result.setCode(1);
            result.setMsg("参数有误，请重试");
            return result;
        }
        //业务逻辑
        try {
            //获取末位序号
            result.setData(systemMenuService.selectEndSort(pid));
        }catch (MenuException e){
            result.setCode(1);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setCode(0);
        return result;
    }

}